﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Dao;
using LinkCard.Model;
using LinkCard.Utils;

namespace LinkCard.ShopCP.Reports
{
    public partial class SystemLog : System.Web.UI.Page
    {
        public string sFromDate = "";
        public string sToDate = "";
        public string sAction = "";
        public DateTime dFromDate = new DateTime(1900, 1, 1);
        public DateTime dToDate = new DateTime(2099, 1, 1);

        private int _totalRecords;

        private int nPageSize = 20;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (Convert.ToBoolean(Session["SHOPADMIN"]) == false)
                {
                    Response.Redirect("/message_box.aspx?msg=5");
                }
            }

            if (!IsPostBack)
                LoadLogs();
        }
        private void LoadLogs()
        {
            var db = new TopLinkEntities();

            Int64 nSupplierID = getSupplierID();
            int curpage = 0;
            int skip = 0;

            if (Request.QueryString["page"] != null)
            {
                if (Request.QueryString["page"].ToString() != "1")
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                    skip = (curpage-1) * nPageSize;
                }
                else
                {
                    curpage = 1;
                    skip = 0;
                }
            }
            else
            {
                curpage = 1;
                skip = 0;
            }

            System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
            dateInfo.ShortDatePattern = "dd/MM/yyyy";

            if (sFromDate != "")
            {
                sFromDate += " 00:00:00";
                dFromDate = Convert.ToDateTime(sFromDate, dateInfo);

            }
            if (sToDate != "")
            {
                sToDate += " 23:59:59";
                dToDate = Convert.ToDateTime(sToDate, dateInfo);
            }

            List<Logs> paylog = new List<Logs>();

            if (sAction == "0")
            {
                paylog = db.Logs.Where(t => t.SupplierID == nSupplierID
                                            && t.Category == "ShopCP"
                                            && t.DateLog >= dFromDate
                                            && t.DateLog <= dToDate)
                                            .OrderByDescending(t => t.DateLog).Skip(skip).Take(nPageSize).ToList();

                _totalRecords = db.Logs.Where(t => t.SupplierID == nSupplierID
                                            && t.Category == "ShopCP"
                                            && t.DateLog >= dFromDate
                                            && t.DateLog <= dToDate)
                                            .OrderByDescending(t => t.DateLog).ToList().Count;
            }
            else
            {
                paylog = db.Logs.Where(t => t.SupplierID == nSupplierID
                                                           && t.Category == "ShopCP"
                                                           && t.DateLog >= dFromDate
                                                           && t.DateLog <= dToDate
                                                           && t.Action.Contains(sAction))
                                                           .OrderByDescending(t => t.DateLog).Skip(skip).Take(nPageSize).ToList();
                _totalRecords = db.Logs.Where(t => t.SupplierID == nSupplierID
                                                           && t.Category == "ShopCP"
                                                           && t.DateLog >= dFromDate
                                                           && t.DateLog <= dToDate
                                                           && t.Action.Contains(sAction))
                                                           .OrderByDescending(t => t.DateLog).ToList().Count;
            }
            

            if (paylog.Count > 0)
            {
                

                int nPageCount;

                if (_totalRecords % nPageSize == 0)
                {
                    nPageCount = _totalRecords / nPageSize;
                }
                else
                {
                    nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                }

                if (nPageCount < 2) divPager.Visible = false;

                PagerShow.Text = LinkCard.Utils.hash1way.ShowPageNavigation(curpage, nPageCount);

                msgList.DataSource = paylog;

                msgList.DataBind();
                msgNoData.Visible = false;
            }
            else
            {

                msgList.DataSource = null;
                msgList.DataSourceID = null;
                msgList.DataBind();
                msgNoData.Visible = true;
            }

        }
        protected Int64 getSupplierID()
        {
            try
            {

                if (Convert.ToInt32(Session["SHOPADMIN"]) == 1) // quanr ly cua hang
                {
                    return ConvertUtility.ToInt64(Session["USER"]);
                }
                else if (Convert.ToInt32(Session["SHOPADMIN"]) == 0)
                {
                    DataTable dt = LinkCard.AdminCP.Common.DataHelper.SelectTable("SELECT SupplierID FROM [ShopStaff] WHERE Username ='" + Session["USER"].ToString() + "'", false);
                    if (dt.Rows.Count > 0)
                    {
                        return ConvertUtility.ToInt64(dt.Rows[0]["SupplierID"]);
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            sFromDate = FromDate.Value.Trim();
            sToDate = ToDate.Value.Trim();
            sAction = this.action.Value.Trim();
            LoadLogs();
        }
    }
}